import { ModalForm, ProFormDigit, ProFormText, ProFormTextArea } from '@ant-design/pro-components';
import { message } from 'antd';
import { refund } from '../../service';
import type { RefundParam } from '../../types.d';

const RefundModal = (props: {
  payOrderNo: string;
  reload: () => void;
  maxAmount: number;
  minAmount?: number;
  orderNo: string;
}) => {
  const { payOrderNo, maxAmount = 0, orderNo, minAmount = -1 } = props;

  return (
    <ModalForm<RefundParam>
      title="退款申请"
      width="35vw"
      layout="horizontal"
      modalProps={{ destroyOnClose: true }}
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      trigger={<a>退款</a>}
      onFinish={async (values) => {
        const res = await refund({
          payOrderNo,
          reason: values.reason,
          refundTo: values.refundTo,
          refundAmount: parseInt((values.refundAmount * 100).toFixed(0)),
          orderNo,
        });
        if (res.success) {
          message.success({ key: 'refund', content: res.message });
          props.reload();
          return true;
        }
        message.error({ key: 'refund', content: res.message });
        return false;
      }}
    >
      <ProFormText label="退款订单" name="orderNo" initialValue={orderNo} disabled required />
      <ProFormText label="退款记录" name="payOrderNo" initialValue={payOrderNo} disabled required />
      <ProFormDigit
        label="退款金额"
        required
        disabled={minAmount != -1}
        name="refundAmount"
        initialValue={(minAmount != -1 ? minAmount : maxAmount) / 100}
        fieldProps={
          minAmount != -1
            ? undefined
            : {
                max: maxAmount / 100,
                min: 0,
                precision: 2,
              }
        }
      />
      <ProFormText label="退款用户" name="refundTo" placeholder="请填写退款用户信息(如手机号)" />
      <ProFormTextArea label="退款原因" name="reason" placeholder="请填写退款原因" />
    </ModalForm>
  );
};

export default RefundModal;
